define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/feiyu/feiyutoec/index' + location.search,
                    add_url: 'zhongzhuan/feiyu/feiyutoec/add',
                    edit_url: 'zhongzhuan/feiyu/feiyutoec/edit',
                    del_url: 'zhongzhuan/feiyu/feiyutoec/del',
                    multi_url: 'zhongzhuan/feiyu/feiyutoec/multi',
                    import_url: 'zhongzhuan/feiyu/feiyutoec/import',
                    table: 'ec_aiuser_to_ecuser_feiyu',
                }
            });

            var table = $("#table");
            
            table.on('post-common-search.bs.table', function (event, table) {

                // 以下2行代码是关键，是加载完表格后，把销售搜索空实例化成selectpage格式
                var form = $("form",table.$commonsearch);
               
                $("input[name='ecid']",form).addClass("selectpage").data("source","zhongzhuan/ec/ecuser/index");
                
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns:true,
                fixedRightNumber: 1,
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),searchable:false},
//                        {field: 'byid', title: __('Byid')},
//                        {field: 'ecid', title: __('Ecid')},
                        
//                        {field: 'callJobId', title:'任务ID',visible: false,searchable:false},
                        {field: 'assign_id', title:'分配计划',visible: false,searchable:false},
                        {field: 'ecid', title: '推送员工',visible:false},
//                        {field: 'calljob.jobName', title: '任务名称', operate: 'LIKE',searchable:false},
                        {field: 'userinfo.name', title: '待推送给员工', operate: 'LIKE',searchable:false,formatter: function(value, row, index) {
                                //value：intro字段的值
                                //row：当前行所有字段的数据
                                //index：当前行索引
                                //示例：
                                if(row.ecid == 0){
                                    return "公海";
                                }else{
                                    return value + "[" + row.userinfo.mobile +"]";
                                }
                                
                            }
                        },
                        {field: 'crmId', title: "客户Crm",searchable:false},
                        {field: 'mobile', title: '客户手机号'},
                        {field: 'name', title: '客户名称',searchable:false},
//                        {field: 'callJobId', title: '任务ID', operate: 'LIKE'},
//                        {field: 'yixiang_value', title: '意向度', operate: 'LIKE',visible:false,searchList: Config.yixiang_list},
//                        {field: 'yixiang_txt', title: __('Yixiang_txt'), operate: 'LIKE',searchable:false},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'label_laiyuan_biaoji_status', title: __('label_laiyuan_biaoji_status'), searchList: {"0":__('label_laiyuan_biaoji_status 0'),"1":__('label_laiyuan_biaoji_status 1')}, formatter: Table.api.formatter.status},
                        {field: 'genjin_status', title: '跟进状态', searchList: {"0":'未跟进',"1":'已跟进'}, formatter: Table.api.formatter.status},
                        {field: 'tuisong_result_txt', title: '推送结果',searchable:false},
                        {field: 'starttime', title: __('Starttime'), operate: 'LIKE',searchable:false},
                        {field: 'updatetime', title: '推送时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'zqdevicestatus',
                                    text: '回传',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'zhongzhuan/feiyu.huichuan/index/crmids/{crmId}' ,
                                    extend: 'data-area=\'["98%","98%"]\'', //这是控制弹窗的弹出的尺寸
                                },
                                {
                                    name: 'detail',
                                    text: '推送',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog btn-ajax btn-restoreit',
                                    title: '我是标题', //按钮显示的文字，支持function
                                    url: '/api/ec.eckehufeiyu/addCustomer', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    confirm: '是否推送该客户？', //点击按钮后的确认框，支持function
                                    success: function (data,rest) {
                                        var opt = {url: ''};
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    },
                                    visible: function (row) {
                                       if(row.status == 0){
                                           return true;
                                       }

                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '变更跟进人',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog btn-ajax btn-restoreit',
                                    title: '我是标题', //按钮显示的文字，支持function
                                    url: '/api/ec.eckehufeiyu/ownUpdateKehuOwner', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    confirm: '是否为该客户变更跟进人？', //点击按钮后的确认框，支持function
                                    success: function (data,rest) {
                                        var opt = {url: ''};
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    },
                                    visible: function (row) {
                                       if(row.status == 2 && row.ecid > 0){
                                           return true;
                                       }
                                       return false;

                                    }
                                },
                                
                                {
                                    name: 'detail',
                                    text: '标记',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog btn-ajax btn-restoreit',
                                    url: '/api/ec.eckehufeiyu/ownUpdateKehuLabel', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    confirm: '是否标记该客户？', //点击按钮后的确认框，支持function
//                                    refresh: true, //操作完成后是否刷新列表
                                    success: function (data,rest) {
                                        var opt = {url: ''};
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    },
                                    visible: function (row) {
                                       if(row.ecid == 0){
                                           return false;
                                       }
                                       if(row.label_laiyuan_biaoji_status != 1){
                                           return true;
                                       }
                                       

                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '跟进记录',
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-assign btn-dialog btn-ajax btn-restoreit',
                                    title: '我是标题', //按钮显示的文字，支持function
                                    url: '/api/ec.eckehufeiyu/saveUserTrajectory', //按钮的链接，支持使用{字段名}来占位替换，支持function
                                    confirm: '是否为该客户添加跟进信息？', //点击按钮后的确认框，支持function
                                    success: function (data,rest) {
                                        var opt = {url: ''};
                                        table.bootstrapTable('refresh', opt);
                                        return true;
                                    },
                                    visible: function (row) {
                                       if(row.status == 1 && row.genjin_status == 0){
                                           return true;
                                       }
                                       return false;

                                    }
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
